﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Common;
using System.Data.SQLite;
using System.Threading;

public class AddSqlite : InterfaceClass
{
    private static List<model> modelList = new List<model>();
    public void Work()
    {
        if (toole.listmodel.Count > 0)
        {
            lock (toole.listmodel)
            {
                modelList = new List<model>(toole.listmodel.ToArray());
                toole.listmodel.Clear();
            }
            Add();
        }
    }

    private static void Add()
    {
        try
        {

            using (SQLiteConnection cnn = new SQLiteConnection(@"Data Source=C:\Project\SQLiteTest\App_Data\test.db3"))
            {
                // 连接数据库
                using (SQLiteCommand cmd = (SQLiteCommand)cnn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO List(ID,IP,date) VALUES(@ID,@IP,@date)";
                    cnn.Open();
                    //using (DbTransaction dbTrans = cnn.BeginTransaction())
                    //{
                        try
                        {
                            lock (modelList)
                            {
                                foreach (model m in modelList)
                                {
                                    cmd.Parameters.AddWithValue("@ID", m.ID);
                                    cmd.Parameters.AddWithValue("@IP", m.IP);
                                    cmd.Parameters.AddWithValue("@date", m.TimeStr);
                                    cmd.ExecuteNonQuery();
                                }
                            }
                            //dbTrans.Commit();
                            modelList.Clear();
                        }
                        catch (Exception ee)
                        {
                            //dbTrans.Rollback();
                            toole.WriteFile(ee.Message + "\r\n" + ee.Source + "\r\n" + ee.StackTrace);
                        }
                    //}
                }
            }
        }
        catch (Exception ee)
        {
            toole.WriteFile(ee.Message + "\r\n" + ee.Source + "\r\n" + ee.StackTrace);
        }
    }
}

